#include<stdio.h>
#include <conio.h>
#include <ctype.h>

char exp[80],stack[80],a,y;
int top=0,ec=0,t=0;
float s[40];
char post[80];

char add(char *x)
{
 if(top!=80) stack[++top]=*x;
}

char delete(char *x)
{
 if(top!=0)
 {
  *x=stack[top];
  stack[top--]='\0';
  }
}

int * isp(char *x,int *p)
{
 if(*x=='$' ){*p=-1; return;}
 if(*x=='^' ){*p=3; return;}
 if(*x=='*' || *x=='/'){*p=2; return;}
 if(*x=='+' || *x=='-'){*p=1; return;}
 if(*x=='(' ){*p=0; return;}
}

int * icp(char *x,int *q)
{
 if(*x=='^' ){*q=4; return;}
 if(*x=='*' || *x=='/'){*q=2; return;}
 if(*x=='+' || *x=='-'){*q=1; return;}
 if(*x=='(' ) {*q=4;return;}
}

void pushstk(char p)
 {
   printf("Value for %c : " ,p);
   scanf("%f",&s[t++]);
 }

void popstk(char p)
 {
  switch(p)
  {
   case '*':s[t-2]*= s[t-1]; break;
   case '/':s[t-2]/= s[t-1]; break;
   case '+':s[t-2]+= s[t-1]; break;
   case '-':s[t-2]-= s[t-1]; break;
  }
  t--;
 }

void main()
{
 int i=1,p,q,value[20],eval[20];

 clrscr();
 printf("---INFIX TO POSTFIX---\n");
 printf("Infix expression:");
 gets(exp);
 printf("\nPostfix expression:- ");

 *(exp+strlen(exp))='#';
 stack[top]='$';
 a=*exp;

 while(a!='#')
 {
    if( a>='a' && a<='z')
      {
       printf("%c",a);
       post[ec++]=a;
      }
   else if(a==')')
   {
       while(stack[top]!='(')
       {
	  delete(&y);
	  printf("%c",y);
	  if(y!='('|| y!=')')post[ec++]=y;
       }
       delete(&y);
   }
   else
   {
    isp(stack[top],&p);
    icp(a,&q);
      while(p>=q)
      {
	delete(&y);
	printf("%c",y);
	if(y!='('|| y!=')')post[ec++]=y;
      }
      add(&a);
   }
   a=*(exp+i);
   i++;
 }

 while(top>0)
 {
  delete(&y);
  printf("%c",y);
  if(y!='('|| y!=')')post[ec++]=y;
 }
  printf("\n\nEvaluation\n");
  for(i=0;i<ec;i++)
  {
    if(isalpha(post[i]))pushstk(post[i]);
    else popstk(post[i]);
  }
  printf("\nExpression evaluates to %0.2f\n",s[0]);
  getch();
}


